var latlng;
var myOptions;
var map;
var markersArray = [];
var requestList;
var elaspeTime;
var timer;
var timer2;
var infowindow;

$(document).ready(function(){
	latlng = new google.maps.LatLng(31.2351145, 121.480418);
	myOptions = {
			zoom: 14,
			center: latlng,
			streetViewControl: false,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

	showRequestList();

	$("#request_type").change(function(){
		selectedRequestId = "";
		showRequestList();
	});
});

function showRequestList() {
	var requestUrl = "./get_requests";
	var params = {
		request_type:$("#request_type").val(),
		page:1,
		page_size:999
	};
	
	$.post(requestUrl, params, function(data){
		var newHtml = "";
		
		clearOverlays();
		requestList = data.resultData.items;
		elaspeTime = 0;
		clearInterval(timer);
		timer = setInterval(function(){elaspeTime += 1000;}, 1000);
		
		for (var i = 0; data.resultData && i < data.resultData.items.length; i++){
			var item = data.resultData.items[i];
			var requestType = (item.type == "0") ? "&lt;求养护&gt;" : "&lt;求帮助&gt;";
			
			latlng = new google.maps.LatLng(item.gpsLocation.split(",")[0], item.gpsLocation.split(",")[1]);
			addMarker(latlng, item.content, map, i);
			
			newHtml += '<div align="left" style="height: 68px; padding-left: 15px; font-size: 13pt; border: 1px solid gray; background: url(../image/bg_request_list.png); cursor: pointer;" onclick="selectRequest(' + i + ')">';
			newHtml += '	<div style="padding-top: 15px;">';
			newHtml += '		<b>' + requestType + '</b>';
			newHtml += item.carModel + " " + item.displacement;
			newHtml +='		</div>';
			newHtml += '	<div>里程： 发布时间：' + item.date.substring(0, 10) + '</div>';
			newHtml += '</div>';
			
			if (selectedRequestId == item.id) {
				selectRequest(i);
			}
		}
		
		$("#div_request_list").html(newHtml);
	}, "json");
}

function selectRequest(index) {
	var item = requestList[index];
	var requestType = (item.type == "0") ? "&lt;求养护&gt;" : "&lt;求帮助&gt;";
	
	selectedRequestId = item.id;
	
	//移动地图中心
	latlng = new google.maps.LatLng(item.gpsLocation.split(",")[0], item.gpsLocation.split(",")[1]);
	map.setCenter(latlng);
	
	var contentString;
	
	clearInterval(timer2);
	
	if (item.type != "0") {

		contentString = '<div id="content" style="width: 330px;height: 320px; font-size: 12pt; overflow-x: hidden;">' +
			'	<div style="line-height: 33px; font-size: 15pt;">' +
			'		<div align="center">求帮助 </div>' +
			'	</div>' +
			'	<div style="line-height: 28px;">发布时间：' + item.date + '</div>' +
			'	<div style="line-height: 28px;">剩余时间：<span id="remain_time" style="color: red;"></div>' +
			'	<div style="line-height: 28px;">' +
			'		客户状态：<span class="blue" style="display: inline-block; width: 130px;">' + ((item.requestStatus == "0") ? "求助中" : "已处理") + "</span>" +
			((item.requestStatus == "0") ? '<img align="top" src="../image/btn_accept_help.png"/>' : '') +
			'	</div>' +
			'	<div style="margin: 10px 0px; border-bottom: 1px dashed gray;"></div>' +
			'	<div style="line-height: 28px;">用　户：' + item.customerName + '</div>'+ 
			'	<div style="line-height: 28px;">电　话：' + item.customerMobile + '</div>' +
			'	<div style="line-height: 28px;">车　牌：' + item.licenseNumber + '</div>' +
			'	<div style="line-height: 28px;">地　址：' + item.address + '</div>' +
			'	<div style="line-height: 28px;">求助详细：</div>' +
			'	<textarea disabled="disabled" style="width: 295px; height: 78px;">' + item.content + '</textarea><br/>' +
			'</div>';
		
		timer2 = setInterval(function(){
			if (eval(item.remainTime) - elaspeTime <= 0) {
				$("#remain_time").html("已过期");
			} else {
				$("#remain_time").html(getTimeStr(eval(item.remainTime) - elaspeTime));
			}
		}, 200);
	} else {
		contentString = '<div id="content" style="width: 330px;height: 320px; font-size: 13pt;">' +
			'	<div style="line-height: 33px; font-size: 15pt;">' +
			'		<img class="pointer fade_effect" src="../image/btn_customer_info.png" align="top" style="margin-right: 40px;" onclick="showCustomerDetail(\'' + item.licenseNumber + '\')"/>' +
			'		<div style="display: inline-block;">' + requestType + '</div>' +
			'	</div>' +
			'	<div style="margin: 10px 0px; border-bottom: 1px dashed gray;"></div>' +
			'	<div style="line-height: 28px;">用　户：' + item.customerName + '</div>'+ 
			'	<div style="line-height: 28px;">电　话：' + item.customerMobile + '</div>' +
			'	<div style="line-height: 28px;">车　型：' + item.carModel + '</div>' +
			'	<div style="line-height: 28px;">里　程：' + item.currentMileage + '</div>' +
			'	<div style="line-height: 28px;">备　注：</div>' +
			'	<textarea disabled="disabled" style="width: 295px; height: 78px;">' + item.content + '</textarea><br/>' +
			'	<div style="line-height: 32px; margin-top: 10px;">发布时间：' + item.date.substring(0, 10) +
			'		<img class="pointer fade_effect" src="../image/btn_affirm_maintain.png" align="top" style="margin-left: 40px;"/>' +
			'	</div>' +
			'</div>';
	}

	if (infowindow) {
		infowindow.close();
	}
	infowindow = new google.maps.InfoWindow({
	    content: contentString
	});
	
	infowindow.open(map, markersArray[index]);
}

function addMarker(positen, title, map, index) {
	var marker = new google.maps.Marker({
	    position: positen,
	    title:title
	});
	
	google.maps.event.addListener(marker, 'click', function() {
		selectRequest(index);
	});
	
	marker.setMap(map);
	markersArray.push(marker);
}

function clearOverlays() {
  if (markersArray) {
	for (i in markersArray) {
	  markersArray[i].setMap(null);
	}
	markersArray.length = 0;
  }
}

function showCustomerDetail(licenseNumber) {
	selectedMaintainOrderLicense = licenseNumber;

	showUserInfo(0, "request_list");
}